package org.jawa.branch;

import org.jawa.core.cache.CacheSizes;
import org.jawa.core.cache.Cacheable;
import org.jawa.core.cache.CannotCalculateSizeException;
import org.jawa.core.exception.NotFoundException;

public class Branch implements Cacheable {

	private static final long serialVersionUID = 7257671490208812805L;
	private Long brId;
	private String brNo;
	private String brName;
	private Long brParentId; //上级机构ID
	private String brStatus;
	private String brRemark;

	transient private Branch parent; //上级机构

	public Branch() {
	}

	public Long getBrId() {
		return brId;
	}

	public void setBrId(Long brId) {
		this.brId = brId;
	}

	public String getBrNo() {
		return brNo;
	}

	public void setBrNo(String brNo) {
		this.brNo = brNo;
	}

	public String getBrName() {
		return brName;
	}

	public void setBrName(String brName) {
		this.brName = brName;
	}

	public Long getBrParentId() {
		return brParentId;
	}

	public void setBrParentId(Long brParentId) {
		this.brParentId = brParentId;
	}

	public String getBrStatus() {
		return brStatus;
	}

	public void setBrStatus(String brStatus) {
		this.brStatus = brStatus;
	}

	public String getBrRemark() {
		return brRemark;
	}

	public void setBrRemark(String brRemark) {
		this.brRemark = brRemark;
	}

	@Override
	public String toString() {
		return brName;
	}

	/**
	 * 返回当前机构的上级机构
	 * @return
	 * @throws NotFoundException
	 */
	public Branch getParent()  {
		synchronized (this) {
			if (parent == null && brParentId != null) {
				try {
					parent = BranchManager.getInstance().getBranch(brParentId);
				} catch (NotFoundException e) {
					//忽略
				}
			}
		}
		return parent;
	}

	public int getCachedSize() throws CannotCalculateSizeException {
		int size = 0;
		size += CacheSizes.sizeOfObject(); // overhead of object
		size += CacheSizes.sizeOfLong(); // userId
		size += CacheSizes.sizeOfLong(); // brId
		size += CacheSizes.sizeOfString(brNo); // brNo
		size += CacheSizes.sizeOfString(brName); // brName
		size += CacheSizes.sizeOfLong(); // brParentId
		size += CacheSizes.sizeOfString(brStatus); // brStatus
		size += CacheSizes.sizeOfString(brRemark); // brRemark
		return size;
	}

}
